<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentitySocial extends CUserIdentity
{

    private $_id;
    public $email;
    public $_name;
    public $_user;
    public $_error_code = 1;

    const ERROR_USER_NOT_ACTIVE = 13;

    public function __construct($email, $password = null)
    {
        parent::__construct($email, $password);


        $this->_user = Users::model()->find('LOWER(email)=:email AND type=:t', array(
            ':email' => strtolower($email),
            ':t'     => Users::TYPE_CUSTOMER
        ));

        if ($this->_user)
        {
            $this->_error_code = self::ERROR_NONE;
            $this->_id         = $this->_user->id;
            $this->_name       = $this->_user->getName();
        }
    }

    public function init()
    {
        Yii::app()->user->setStateKeyPrefix('_cus');
    }

    /**
     * Authenticates a user.
     * @return boolean whether authentication succeeds.
     */
    public function authenticate()
    {
        return $this->_error_code;
    }

    /**
     * @return integer the ID of the user record
     */
    public function getId()
    {
        return $this->_id;
    }

    public function getName()
    {
        return $this->_name;
    }

}
